import { successResp, DATE_FMT } from '../../core/common'
import dayjs from 'dayjs'

import MockStore from '../../ipc/mockstore'

/**
 * mock文件有两个作用，一是作为和C++桥接的接口定义，二是为支持前后段分离的前端调试
 *
 * API reference
 * https://esti-bd.yuque.com/teid1h/project_doc/wbfx2ngp58epe8mg#cDOJx
 */
class MyMockStore extends MockStore {
  constructor() {
    super('[curve]');
  }

  initialize() {
    const PLAN_LIST = [
      {
        "id": 1,
        "name": "交流BC线电压曲线",
        "picker_type": "COMPRANGE",
        "mode_type": "HOUR_BAR",
        "data_interval": 5,
        "interval_unit": "MIN_UNIT",
        "point_list": "r262:ai:0,r262:ai:1,r262:ai:2"
      },
      {
        "id": 2,
        "name": "交流AB线电压曲线",
        "picker_type": "RANGE",
        "mode_type": "GENERAL_CURVE",
        "data_interval": 5,
        "interval_unit": "MIN_UNIT",
        "point_list": "r262:ai:0,r262:ai:1,r262:ai:2"
      }
    ];

    this.setJson('PLAN_LIST', PLAN_LIST);
  }
}
const store = new MyMockStore();

function randomValue(time, isBar) {
  const v = 220 + (Math.random() - 0.5) * 100;
  return {
    time: isBar ? time.format(DATE_FMT) : String(time.valueOf()),
    value: v.toFixed(2),
  }
}

const CURVE_DATA_FUN = (param, isBar = false) => {
  const { picker_type, data_interval, interval_unit, from, to, point_list } = param;
  const pids = point_list.split(',');
  const unit = interval_unit === 'SEC_UNIT' ? 'second' : 'minute';
  const list = [];

  if (picker_type === 'COMPRANGE') {

    let begin = dayjs(from);
    const end = dayjs(to);
    while (begin.isBefore(end)) {
      for (const point_id of pids) {
        list.push({
          point_id: point_id,
          point_desc: `交流线${point_id}电压`,
          device_desc: "#1PCS",
          bay_desc: "#1PCS",
          station_desc: "朗山储能",
          date: begin.format('YYYY-MM-DD'),
          values: [],
        });
      }
      begin = begin.add(1, 'day');
    }

    list.forEach(point => {
      let begin = dayjs(point.date, 'YYYY-MM-DD');
      const end = begin.add(1, 'day');
      while (begin.isBefore(end)) {
        point.values.push(randomValue(begin, isBar));
        begin = begin.add(data_interval, unit);
      }
    });

  } else {
    pids.forEach(point_id => {
      list.push({
        point_id: point_id,
        point_desc: `交流线${point_id}电压`,
        device_desc: "#1PCS",
        bay_desc: "#1PCS",
        station_desc: "朗山储能",
        values: [],
      });
    });

    list.forEach(point => {
      let begin = dayjs(from);
      const end = dayjs(to);
      while (begin.isBefore(end)) {
        point.values.push(randomValue(begin, isBar));
        begin = begin.add(data_interval, unit);
      }
    });
  }

  return successResp({ point_list: list });
}

export const mock = {
  contexts: {
    text: "XX站XX设备",
    id: 'r221:ai:1'
  },
  showTrigger: true,
  signals: {
  },
  slots: {
    request_get_model_node_list: {
      delay: 0,
      response: successResp({
        "model_node_list": [
          {
            "station_id": "sda:lsc",
            "station_desc": "郎山储能",
            "bay_list": [
              {
                "bay_id": 1004,
                "bay_desc": "#1PCS",
                "device_list": [
                  {
                    "device_id": 1800,
                    "device_desc": "#1PCS",
                  }
                ]
              },
              {
                "bay_id": 1005,
                "bay_desc": "#2PCS",
                "device_list": [
                  {
                    "device_id": 1801,
                    "device_desc": "#2PCS",
                  }
                ]
              }
            ]
          }
        ]
      })
    },

    request_get_point_list: {
      delay: 1000,
      response: (param) => {
        const { data_type, device_id } = param;
        const prefix = data_type + ':' + device_id + ':r220:ai:';
        const list = [];
        for (let i = 0; i < 200; i++) {
          list.push({ point_id: prefix + i, point_desc: `系统电压${i}相`, point_type: 127 },);
        }
        return successResp({ point_list: list });
      }
    },

    request_get_general_data_curve: {
      delay: 1000,
      response: (param) => {
        const { picker_type, data_interval, interval_unit, from, to, point_list } = param;
        return CURVE_DATA_FUN({
          picker_type, data_interval, interval_unit, from, to, point_list,
        });
      }
    },
    request_get_second_data_curve: {
      delay: 1000,
      response: (param) => {
        const { picker_type, from, to, point_list } = param;
        const data_interval = 1;
        const interval_unit = 'SEC_UNIT';
        return CURVE_DATA_FUN({
          picker_type, data_interval, interval_unit, from, to, point_list,
        });
      }
    },
    request_get_data_bar: {
      delay: 1000,
      response: (param) => {
        const { picker_type, mode_type, from, to, point_list } = param;
        let data_interval;
        let interval_unit = 'MIN_UNIT';
        if (mode_type === 'HOUR_BAR') {
          data_interval = 60;
        } else if (mode_type === 'DAY_BAR') {
          data_interval = 60 * 24;
        }
        return CURVE_DATA_FUN({
          picker_type, from, to, point_list,
          interval_unit, data_interval,
        }, true);
      }
    },

    request_get_curve_plan_list: {
      delay: 1000,
      response: () => {
        const plan_list = store.getJson('PLAN_LIST');
        return successResp({ "curve_plan_list": plan_list });
      }
    },
    request_insert_curve_plan: {
      delay: 1000,
      response: (param) => {
        const { name, picker_type, mode_type, data_interval, interval_unit, point_list } = param;

        const id = Math.floor(Math.random() * 10000);
        const plan = { id, name, picker_type, mode_type, data_interval, interval_unit, point_list }
        const plan_list = store.getJson('PLAN_LIST');
        plan_list.push(plan);
        store.setJson('PLAN_LIST', plan_list);
        return successResp(plan);
      }
    },
    request_delete_curve_plan: {
      delay: 1000,
      response: (param) => {
        const { id } = param;

        const plan_list = store.getJson('PLAN_LIST');
        const index = plan_list.findIndex(plan => plan.id === id);
        if (index === -1) {
          throw new Error('plan not found');
        }
        plan_list.splice(index, 1);
        store.setJson('PLAN_LIST', plan_list);
        return successResp();
      }
    },
    request_update_curve_plan: {
      delay: 1000,
      response: (param) => {
        const { id, name, picker_type, mode_type, data_interval, interval_unit, point_list } = param;

        const plan_list = store.getJson('PLAN_LIST');
        const plan = plan_list.find(plan => plan.id === id);
        if (!plan) {
          throw new Error('plan not found');
        }
        Object.assign(plan,
          { name, picker_type, mode_type, data_interval, interval_unit, point_list });
        store.setJson('PLAN_LIST', plan_list);
        return successResp();
      }
    },
  }
};